<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

if (!isset($project) && !($project instanceof LM_Table_Project)) {
    LM::redirect();
}

if (!$project->hasRounds()) {
    LM::redirect("rounds", "Create some Rounds before entering the matrix!");
}

$rid       = LM::getSessionValueFromRequest("_filterresultsbyround_", "rid", $project->getCurrentRound());
$matchdate = LM::getSessionValueFromRequest("_matchdate_", "match_date", "0000-00-00");
$matchtime = LM::getSessionValueFromRequest("_matchtime_", "match_time", "00:00");

switch ($task) {

    case 'creatematch':

        $home = intval(mosGetParam($_POST, "match_team1", 0));
        $away = intval(mosGetParam($_POST, "match_team2", 0));

        $projectmanager = new Projectmanager($project);
        $projectmanager->createMatch($home, $away, $rid, $matchdate, $matchtime);
        break;

    case 'deletematch':

        $m2d = intval(mosGetParam($_POST, "match2delete", 0));
        if ($m2d) {
            $projectmanager = new Projectmanager($project);
            $projectmanager->deleteMatch($m2d);
        }

        break;

    case 'invert':
        $matchtable = LM::getLMTable("Match");
        $matchtable->filterbyProject($project->getID());
        $matchtable->filterbyRound(intval($rid));
        $matches = $matchtable->getRows();

        $rid2 = intval(mosGetParam($_POST, "rid2", 0));
        if ($rid2 > 0 && $rid2 != $rid) {

            $projectmanager = new Projectmanager($project);

            foreach ($matches AS $match) {

                $home = $match->team2_id;
                $away = $match->team1_id;

                $projectmanager->createMatch($home, $away, $rid2);
            }
        }

        mosRedirect("index2.php?option=com_leagueman&act=matrix&rid=$rid2");
        exit();

        break;
}

$matchtable = LM::getLMTable("Match");
$matchtable->filterbyProject($project->getID());
$matchtable->filterbyRound($rid);
$matches = $matchtable->getRows();

$match_list = array();
foreach ($matches as $match) {
    $key = $match->team1_id . '_' . $match->team2_id;
    $match_list[$key] = $match->match_id;
}

$teams = $project->getTeams();

$matrix = '<table width="100%" class="adminform" border="1">';

$k = 0;
for ($rows = 0; $rows <= count($teams); $rows++) {

    $trow = ($rows == 0) ? $teams[0] : $teams[$rows - 1];

    $matrix .= "<tr class=\"row$k\">";

    for ($cols = 0; $cols <= count($teams); $cols++) {
        $text = "";
        $checked = 0;
        $color = "white";
        $onClick = "";

        $tcol = ( $cols == 0 ) ? $teams[0] : $teams[$cols - 1];

        $match = $trow->team_id . '_' . $tcol->team_id;

        $mid = 0;
        if (array_key_exists($match, $match_list)) {
            $checked = 1;
            $color = 'teal';
            $mid = $match_list[$match];
        }
        if ($rows == 0 && $cols == 0)
            $text = '<td width="180"align="center">&nbsp;</td>';
        else if ($rows == 0)
            $text = sprintf('<td width="20" title="%s">&nbsp;</td>', $tcol->team_name); //picture columns
        else if ($cols == 0)
            $text = sprintf('<td width="180" align="right" nowrap="nowrap"><b>%s</b></td>', $trow->team_name); // named rows
        else if ($rows == $cols)
            $text = '<td width="20" bgcolor="#A0A0A4">&nbsp;</td>'; //impossible matches
        else {
            if (!$checked) {
                $action = "Create Match: ";
                $onClick = sprintf("onClick=\"javascript:createMatch('%s','%s');\"", $trow->team_id, $tcol->team_id);
            } else {
                $action = "Delete Match: ";
                $onClick = sprintf("onClick=\"javascript:deleteMatch('%s');\"", $mid);
            }
            $text = sprintf("<td bgcolor=\"%s\" onmouseover=\"this.T_WIDTH=400;this.T_DELAY=50;return escape('%s <h2>%s - %s</h2>')\" align=\"center\"><input type=\"radio\" name=\"matches[]\" %s /></td>\n", $color, $action, addslashes($trow->team_name), addslashes($tcol->team_name), $onClick);
        }
        $matrix .= $text;
    }

    $k = 1 - $k;
    $matrix .= '<td nowrap="nowrap"></td></tr>';
}

$matrix .= '</table>';

$template = LM::getTemplate();
$template->assign("view", "admin/matrix.tpl.php");
$template->assign("project", $project);
$template->assign("team", $team);
$template->assign("rid", $rid);
$template->assign("match_date", $matchdate);
$template->assign("match_time", $matchtime);
$template->assign("matrix", $matrix);
$template->assign("rounds", $project->getRoundBox());
$template->assign("my", $my);

$template->display("admin/view.tpl.php");